﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.ManifestInfos;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.MaterialManagement.Add
{
    public class RowBillMaterialCreateHandler : IRequestHandler<RowBillMaterialCreateCommon, Result>
    {
        private readonly IBaseRepository<ItemManifestRow> ItemManifestRowRepository;
        public IMapper mapper { get; set; }
        public RowBillMaterialCreateHandler(IBaseRepository<ItemManifestRow> itemManifestRowRepository, IMapper mapper)
        {
            ItemManifestRowRepository = itemManifestRowRepository;
            this.mapper = mapper;
        }
        public async Task<Result> Handle(RowBillMaterialCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Added };
            var AddData = mapper.Map<ItemManifestRow>(request);
            string sql = "insert into ItemManifestRow values(@RowId,@RowOrder,@Process,@ChooseId,@RowAttribute,@SpecName,@Version,@LeadPeriod,@Num,@RowState,@Handle,@HandleContext,@HandleTime,@Pid,@IsDelete);";
            var AddNum = ItemManifestRowRepository.Add(sql, new
            {
                AddData.RowId,
                AddData.RowOrder,
                AddData.Process,
                AddData.ChooseId,
                AddData.RowAttribute,
                AddData.SpecName,
                AddData.Version,
                request.LeadPeriod,
                AddData.Num,
                AddData.RowState,
                AddData.Handle,
                AddData.HandleContext,
                AddData.HandleTime,
                AddData.Pid,
                AddData.IsDelete,
            });
            if (AddNum > 0)
            {
                result.Code = (int)CommonErrorCodes.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
